import axios from "axios";
import { ElMessage } from 'element-plus'
const baseURL = import.meta.env.VITE_APP_BASE_API;
const env = import.meta.env.VITE_NODE_ENV
const axiosInstance = axios.create({
  baseURL: env == 'development'? '/api': baseURL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
})

axiosInstance.interceptors.request.use(
  config => {
    if (localStorage.getItem('authToken')) {
      config.headers.token = localStorage.getItem('authToken');
    }
    return config;
  },
  error => Promise.reject(error)
)

axiosInstance.interceptors.response.use(
  response => {
    // 对响应数据做些处理
    return response.data;
  },
  error => {
    // 对响应错误做些处理
    if (error.response.status === 401) {
      // 处理未授权的情况，如跳转登录页面
      ElMessage.error(error.response.data.message)
    }else{
      ElMessage.error(error.response.data.message)
    }
    return Promise.reject(error);
  }
);

export default axiosInstance;